#！/bin/bash
#################################################################
# @Description: 用例名称：存储管理-RAID支持，测试目的：测试操作系统支持软件raid6
# @Date : 2023-11-22
# @Last Modified by :
# @Last Modified time:
#################################################################
res=0
mdadm_installed=1
bc_installed=1
disk_size_min=4
raid_level="/dev/md6"

#0.安装mdadm软件包
echo "---该用例测试操作系统支持软raid6,请确保机器上有空闲容量大于4G的磁盘---"
echo "0.安装mdadm和bc软件包"
yum install mdadm -y | grep "Installed:" > /dev/null 2>&1 && mdadm_installed=0
[ "$mdadm_installed" = "0" ] && echo "mdadm软件包安装成功" || echo "mdadm软件包已安装"
yum install bc -y | grep "Installed:" > /dev/null 2>&1 && bc_installed=0
[ "$bc_installed" = "0" ] && echo "bc包安装成功" || echo "bc软件包已安装"

#1.获取机器上未使用的空闲磁盘
echo ""
echo "1.请您输入机器上大于${disk_size_min}G的空闲磁盘：(格式:/dev/sdb或/dev/hdb)"
read -p "> " disk
if [ -z "$disk" ]; then
	echo "没有输入空闲磁盘名称,退出脚本"
	exit 1
fi
disk=$(echo $disk | awk -F '/' '{print $3}')
num=$(lsblk | grep $disk | wc -l)
if [[ $num > 1 ]]; then
	echo "---------$disk磁盘分区---------"
	lsblk | grep $disk
	echo "---------$disk磁盘分区---------"
	echo "${disk}磁盘有分区,请格式化该分区后重新运行脚本"
	exit 1
elif [[ $num < 1 ]]; then
	echo "请您重新运行脚本输入一块正确的空闲磁盘名称"
	exit 1
fi
disk_size=$(lsblk --bytes | grep $disk | awk '{sum = $4/1024/1024/1024} END {printf sum}')
if (( $(echo "$disk_size <= $disk_size_min" | bc -l) )); then
	echo "空闲磁盘容量不大于${disk_size_min}G,不满足测试要求"
	exit 1
fi
disk=/dev/$disk
disk_last="$(echo "${disk}" | awk -F '/' '{print $NF}')"
echo "本次测试使用的空闲磁盘为:${disk}"
echo "--------------------所有磁盘--------------------"
lsblk
echo "--------------------所有磁盘--------------------"

#2.磁盘分区
echo ""
echo "2.磁盘分区"
echo "n
p
1

+1G
Y
n
p
2

+1G
Y
n
p
3

+1G
Y
n
p
4

+1G
Y
w" | fdisk "${disk}" > /dev/null 2>&1 || res=2
[ "$res" = "0" ] && echo "磁盘${disk}分区成功!" || exit 1
echo "${disk}磁盘分区成功!"
echo "--------------------${disk_last}磁盘分区情况--------------------"
lsblk | grep "${disk_last}"
echo "--------------------${disk_last}磁盘分区情况--------------------"
sleep 3

#3.将分区组成raid6
echo ""
echo "3.将${disk}分区组成raid6"
echo ${disk} | grep nvme > /dev/null 2>&1
if [ $? -eq 0 ];then
	echo y | mdadm -C ${raid_level} -l 6 -n 4 "${disk}"p1 "${disk}"p2 "${disk}"p3 "${disk}"p4 > /dev/null 2>&1 || res=3
else
	echo y | mdadm -C ${raid_level} -l 6 -n 4 "${disk}"1 "${disk}"2 "${disk}"3 "${disk}"4 > /dev/null 2>&1 || res=3
fi
[ "$res" = "0" ] && echo "将${disk}分区组成raid6成功!" || exit 1
echo "--------------------${disk_last}组raid情况--------------------"
lsblk | grep -E "${disk_last}|md6"
echo "--------------------${disk_last}组raid情况--------------------"
echo "--------------------raid详情--------------------"
mdadm --detail ${raid_level}
echo "--------------------raid详情--------------------"

#4.将raid6格式化并挂载
echo ""
echo "4.格式化${raid_level}为ext4文件系统并挂载"
echo "格式化${raid_level}为ext4文件系统"
mkfs.ext4 -F ${raid_level} > /dev/null 2>&1 || res=4
[ "$res" = "0" ] && echo "格式化${raid_level}成功!" || exit 1
echo "挂载${raid_level}到/mnt/raid6目录"
mkdir /mnt/raid6 > /dev/null 2>&1
mount ${raid_level} /mnt/raid6 > /dev/null 2>&1 || res=4
[ "$res" = "0" ] && echo "挂载${raid_level}成功!" || exit 1
echo "--------------------raid挂载情况--------------------"
lsblk | grep -E "${disk_last}|md6"
echo "--------------------raid挂载情况--------------------"
echo "创建/mnt/raid6下test文件并查看,测试raid6读写功能"
touch /mnt/raid6/test || res=4
[ "$res" = "0" ] && echo "创建/mnt/raid6下test文件成功!" || exit 1
echo "/mnt/raid6目录下文件如下:"
ls /mnt/raid6

#5.环境清理
echo ""
echo "5.环境清理"
echo "卸载/mnt/raid6"
umount /mnt/raid6 > /dev/null 2>&1
rm -rf /mnt/raid6 > /dev/null 2>&1
echo "停止raid6并清除raid超级快信息"
echo ${disk} | grep nvme > /dev/null 2>&1
if [ $? -eq 0 ];then
	mdadm --stop ${raid_level} > /dev/null 2>&1
	sleep 2
	mdadm --misc --zero-superblock "${disk}"p1 > /dev/null 2>&1
	mdadm --misc --zero-superblock "${disk}"p2 > /dev/null 2>&1
	mdadm --misc --zero-superblock "${disk}"p3 > /dev/null 2>&1
	mdadm --misc --zero-superblock "${disk}"p4 > /dev/null 2>&1
else
	mdadm --stop ${raid_level} > /dev/null 2>&1
	sleep 2
	mdadm --misc --zero-superblock "${disk}"1 > /dev/null 2>&1
	mdadm --misc --zero-superblock "${disk}"2 > /dev/null 2>&1
	mdadm --misc --zero-superblock "${disk}"3 > /dev/null 2>&1
	mdadm --misc --zero-superblock "${disk}"4 > /dev/null 2>&1
fi
echo "删除${disk}分区"
echo "d

d

d

d
w" | fdisk ${disk} > /dev/null 2>&1
wipefs -a "${disk}" > /dev/null 2>&1
[ "$mdadm_installed" = "0" ] && yum remove mdadm -y > /dev/null 2>&1 && echo "mdadm软件包卸载成功"
[ "$bc_installed" = "0" ] && yum remove bc -y > /dev/null 2>&1 && echo "bc软件包卸载成功"
[ "$res" = "0" ] && echo -e "\033[32m-----------------test PASSED----------------\033[0m" || echo -e "\033[31m-----------------test FAILD----------------\033[0m"
exit $res
